checkCookie()
//获取banner
async function getBanner() {
    let data = await myPromise({
        method: 'get',
        headers: {
            'x-platform': 'pc'
        },
        url: 'http://api.yuguoxy.com/api/shop/banner',
        // data: {
        //     table: `${area}`
        // }
    })
    let bannerArr = data.resultInfo.list;
    let oSwipers = document.querySelectorAll('.swiper-slide>img');
    for (let i = 0; i < bannerArr.length; i++) {
        oSwipers[i + 1].setAttribute('src', `${bannerArr[i].url}`);
    }
    oSwipers[0].setAttribute('src', `${bannerArr[bannerArr.length-1].url}`);
    oSwipers[oSwipers.length - 1].setAttribute('src', `${bannerArr[0].url}`);
}


//渲染商品
//全局商品数组
let globalProduct = [];
async function onload(no = 1) {
    let data = await myPromise({
        method: 'get',
        url: 'http://api.yuguoxy.com/api/shop/list',
        data: {
            "pageNo": no,
            "pageSize": 8
        }
    })

    let oProducts = document.querySelector('.product');
    let productList = data.resultInfo.list;
    console.log(productList);

    productList.forEach(item => {
        globalProduct.push(item);
    })

    let str = '';
    let arr = ['天猫', '淘宝'];

    globalProduct.forEach(item => {
        let yuan = parseInt(item.price);
        let jiao = parseInt(item.price % 10);
        str += `<div class="tab fl" onclick="goDetail(${item.id})">
                    <img src="${item.picture}" alt="">
                    <p>${item.shop}</p>
                    <div class="info clearfix">
                        <div class="fl"></div>
                        <span class="fr">包邮</span>
                    </div>
                    <div class = "bottom clearfix">
                        <div class="fl">
                            <span>￥</span>
                            <span>${yuan}</span>
                            <span>.${jiao}</span>
                            <span>${item.oldprice}人已买</span>
                        </div>
                        <div class="label fr">1元券</div>
                    </div>
                </div>
        `;
    });

    oProducts.innerHTML = str;

    let oDivs = document.querySelectorAll('.info>div');
    oDivs.forEach(item => {
        let num = getRandom(0, arr.length - 1);
        let content = arr[num];
        if (content == '淘宝') {
            item.innerHTML = content;
            item.classList.add('bgOrange');
        } else if (content == '天猫') {
            item.innerHTML = content;
            item.classList.add('bgRed');
        }
    })

}


//查看更多
function getMore() {
    let count = 1;
    let oBtn = document.querySelector('.more');
    oBtn.addEventListener('click', function () {
        count++;
        if (count == 5) {
            oBtn.style.visibility = 'hidden';
        }
        onload(count)
    });
}


// 回到顶部
function backTop() {
    let oDiv = document.querySelector('.backTop');

    oDiv.addEventListener('click', function () {
        var timer = setInterval(function () {
            document.documentElement.scrollTop -= 100;
            if (document.documentElement.scrollTop <= 0) {
                clearInterval(timer);
            }
        }, 20);
    });

    //窗口滚动是否显示返回顶部
    document.addEventListener('scroll', function () {
        var h = window.scrollY;
        if (h > 500) {
            oDiv.style.display = "flex";
        } else {
            oDiv.style.display = "none";
        }
    })
}

function goDetail(id) {
    window.location.href = `detail.html?id=${id}`;
}


function checkCookie() {
    if (!getCookie('username')) {
        alert('请先登录');
        window.location.href = "login.html";
    }
    let nameSpan = document.querySelector('.nameSpan');
    nameSpan.innerHTML = 'hello, ' + getCookie('username');
}

function quit() {
    let quit = document.querySelector('a[class="quit"]');
    quit.addEventListener('click', function () {
        removeCookie('username');
        window.location.href = 'login.html';
    });
}


getBanner()
onload()
getMore()
backTop()
quit()